package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.PavilionSortVo;
import com.indexingsystem.boss.Vo.PavilionsQueryVo;
import com.indexingsystem.boss.Vo.PavilionsReqVo;
import com.indexingsystem.boss.admin.mapper.PavilionsMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.PavilionMenuRelations;
import com.indexingsystem.boss.entity.Pavilions;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.StringUtil;
import com.indexingsystem.system.service.IPavilionsService;

@Service
public class PavilionsServiceImpl implements IPavilionsService {
	
	@Resource
	private PavilionsMapper pavilionsMapper;
	
	@Override
	public Pavilions selectPavilionName(String name) {
		return pavilionsMapper.selectPavilionName(name);
	}

	@Override
	public Pavilions selectPavilionsByPrimaryKey(String pavilionId) {
		return pavilionsMapper.selectPavilionsByPrimaryKey(pavilionId);
	}
	
	public List<PavilionMenuRelations> selectPavilionMenuRelationsByPrimaryKey(String pavilionId) {
		return pavilionsMapper.selectPavilionMenuRelationsByPrimaryKey(pavilionId);
	}
	
	@Override
	public int insertPavilionMenuRelations(PavilionsReqVo pavilionsReqVo) throws Exception{
		int result = 0;
	    Pavilions pavilions = pavilionsReqVo.getPavilions();
	    if(null != pavilions){
	        result =pavilionsMapper.insertPavilions(pavilions);
	    }
		
		List<PavilionMenuRelations> pavilionMenuRelations = pavilionsReqVo.getPavilionMenuRelationsList();
		Set<PavilionMenuRelations> pavilionMenuRelaSet = null;
		List<PavilionMenuRelations> addPavilionMenuRelations = null;
		PavilionMenuRelations pavilionMenuRelation = null;
		if(null != pavilionMenuRelations && pavilionMenuRelations.size() > 0){
		    pavilionMenuRelaSet = new HashSet<PavilionMenuRelations>();
		    for (PavilionMenuRelations pavilionMenu : pavilionMenuRelations) {
	            pavilionMenuRelation = new PavilionMenuRelations();
	            pavilionMenuRelation.setPavilionId(pavilions.getPavilionId());
	            pavilionMenuRelation.setPavilionName(pavilions.getPavilionName());
	            pavilionMenuRelation.setMenuId(pavilionMenu.getMenuId());
	            pavilionMenuRelation.setMenuName(pavilionMenu.getMenuName());
	            
	            if(!pavilionMenuRelaSet.contains(pavilionMenuRelation)){
	                pavilionMenuRelaSet.add(pavilionMenuRelation);
	            }
	        }		    
		    if(null != pavilionMenuRelaSet && pavilionMenuRelaSet.size()>0){
	            addPavilionMenuRelations = new ArrayList<PavilionMenuRelations>();
		        for(PavilionMenuRelations pavilionSet:pavilionMenuRelaSet){
		            pavilionSet.setPavilionMenuId(StringUtil.createSystemDataPrimaryKey());
		            addPavilionMenuRelations.add(pavilionSet);
		        }
		    }
		}
		if(null != addPavilionMenuRelations && addPavilionMenuRelations.size() > 0){
			pavilionsMapper.insertPavilionMenuRelations(addPavilionMenuRelations);
		}	
		return result;
	}
	
	@Override
	public int updatePavilions(PavilionsReqVo pavilionsReqVo) throws Exception {
	    int result = 0;
	    if(null != pavilionsReqVo){
	        List<String> idList =null;
	        PavilionMenuRelations pavilionMenuRelation = null;
	        Set<PavilionMenuRelations> pavilionMenuRelaSet = null;
	        List<PavilionMenuRelations> addPavilionMenuRelations = null;
	        Pavilions pavilions = pavilionsReqVo.getPavilions();
	        if(null != pavilions){
	            List<PavilionMenuRelations> pavilionMenuRelations = pavilionsReqVo.getPavilionMenuRelationsList();
	            if(null != pavilionMenuRelations && pavilionMenuRelations.size()>0){
	                pavilionMenuRelaSet = new HashSet<PavilionMenuRelations>();
	                idList = new ArrayList<String>();
	                idList.add(pavilions.getPavilionId());
	                for (PavilionMenuRelations menuRelation : pavilionMenuRelations) {
	                    pavilionMenuRelation = new PavilionMenuRelations();
	                    pavilionMenuRelation.setPavilionId(pavilions.getPavilionId());
	                    pavilionMenuRelation.setPavilionName(pavilions.getPavilionName());
	                    pavilionMenuRelation.setMenuId(menuRelation.getMenuId());
	                    pavilionMenuRelation.setMenuName(menuRelation.getMenuName());
	                    
	                    if(!pavilionMenuRelaSet.contains(pavilionMenuRelation)){
	                        pavilionMenuRelaSet.add(pavilionMenuRelation);
	                    }
	                }
	                
	                if(null != pavilionMenuRelaSet && pavilionMenuRelaSet.size()>0){
	                    addPavilionMenuRelations = new ArrayList<PavilionMenuRelations>();
	                    for(PavilionMenuRelations pavilionSet:pavilionMenuRelaSet){
	                        pavilionSet.setPavilionMenuId(StringUtil.createSystemDataPrimaryKey());
	                        addPavilionMenuRelations.add(pavilionSet);
	                    }
	                }
	            }
	            if(null != idList && idList.size()>0){
	                pavilionsMapper.deletePavilionMenuRelations(idList);
	            }
	            if(null != addPavilionMenuRelations && addPavilionMenuRelations.size()>0){
	                pavilionsMapper.insertPavilionMenuRelations(addPavilionMenuRelations);
	            }
	            result = pavilionsMapper.updatePavilions(pavilions);	            
	        }  	      
	    }
	    return result;		
	}

	@Override
	public PagedResult<Pavilions> selectPavilionsList(PavilionsQueryVo pavilionsQueryVo) {
		Integer pageNo = pavilionsQueryVo.getPageNumber() == null ? 1 : pavilionsQueryVo.getPageNumber();
		Integer pageSize = pavilionsQueryVo.getPageSize() == null ? 10 : pavilionsQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(pavilionsMapper.selectPavilionsList(pavilionsQueryVo));
	}

	@Override
	public Integer getPavilionsListCount(PavilionsQueryVo pavilionsQueryVo) {
		return pavilionsMapper.getPavilionsListCount(pavilionsQueryVo);
	}

	@Override
	public int deletePavilions(List<String> ids)  throws Exception{
	    int result = 0;
	    if(null != ids && ids.size()>0){
	        result ++;
	        pavilionsMapper.deletePavilionMenuRelations(ids);
	        pavilionsMapper.deletePavilions(ids);
	    }
	    return result;		
	}
	
	
	   /**
     * 根据id删除分馆信息.
     * @param id
     * @return
     */
	@Override
    public int deletePavilionsById(String id){
	    return pavilionsMapper.deletePavilionsById(id);
    }
	
	/**
	 * 获取分馆最大排序值.
	 */
	 @Override
	 public String getParentColumnOrderSortMax() {
	      return pavilionsMapper.getParentColumnOrderSortMax();
	}
	 
	/**
	 * 通过排序获取分馆信息.
	 * @param columnOrder
	 * @return
	 */
	 @Override
	 public Pavilions getPavilionByColumnOrder(String columnOrder){
	     return pavilionsMapper.getPavilionByColumnOrder(columnOrder);
	 }
	 
	 /**
	  * 批量修改菜单栏目上移、下移操作.
	  * @param list
	  * @return
	  * @throws Exception
	  */
	  @Override
	  public int updateBatchPavilions(List<Pavilions> list) throws Exception{
	       return pavilionsMapper.updateBatchPavilions(list);
	  }
	  
	  
	  /**
	     * 通过分馆idlist 查询 所有排序值列表.
	     * 
	     * @param ids
	     * @return
	     */
	  @Override
	  public List<String> getPavilionsListByIds(List<String> ids){
	      return pavilionsMapper.getPavilionsListByIds(ids);
	  }
	  
	  
	/**
	 * 通过排序获取分馆列表.
	 * @param columnOrder
	 * @return
	 */
	 @Override
	 public List<Pavilions> getPavilionsListByColumnOrder(String columnOrder){
	        return pavilionsMapper.getPavilionsListByColumnOrder(columnOrder);
	 }

	@Override
	public List<Pavilions> getPavilionListByCondition(PavilionSortVo pavilionSortVo) {
		  return pavilionsMapper.getPavilionListByCondition(pavilionSortVo);
	}

	@Override
	public void updatePavilionListByCondition(PavilionSortVo pavilionSortVo) {
		   pavilionsMapper.updatePavilionListByCondition(pavilionSortVo);
		
	}
}